package com.cheng.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Author: Cheng
 * Date: 2019/8/25
 * Time: 9:51
 * Description: 时间转换类 jdk8
 */
public class DateUtil {
//    注意大写HH是24小时制 小写为12小时制
    static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    static final String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 获取东8区当前时间
     * @return
     */
    public static Date getDate() {
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("+08:00"));
        String formatNow = now.format(DateTimeFormatter.ofPattern(TIME_PATTERN));
        Date parse = null;
        try {
            parse = new SimpleDateFormat(TIME_PATTERN).parse(formatNow);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static String getDateStr(){
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("+08:00"));
        return now.format(DateTimeFormatter.ofPattern(DATE_PATTERN));
    }
}
